using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Media;

namespace MVExplorer.Shapes
{
	class ShapeArrow : ShapeBase
	{
		private const int ARROWHEAD_WIDTH = 10;
		private const int ARROWHEAD_LENGTH = 10;

		public override DrawingMode ShapeMode
		{
			get
			{
				return DrawingMode.Arrow;
			}
		}

		public override System.Windows.Shapes.Shape CreatePreElement()
		{
			Polyline poly = new Polyline();
			poly.Stroke = SystemColors.WindowTextBrush;
			poly.StrokeThickness = 1;
			poly.Width = 0;
			poly.Height = 0;
			
			return poly;
		}

		public override void MoveElement(Shape element, Point from, Point to)
		{
			base.MoveElement(element, from, to);

			Polyline poly = element as Polyline;
			Debug.Assert(poly != null);
			if (poly == null)
				return;
			double radians = Math.Atan2(to.Y - from.Y, to.X - from.X);
			double angle = radians * (180/Math.PI);
			poly.StrokeEndLineCap = PenLineCap.Triangle;
			poly.StrokeLineJoin = PenLineJoin.Bevel;

			Point ptCenter = new Point(0, ARROWHEAD_WIDTH / 2 + poly.StrokeThickness);
			poly.RenderTransform = new RotateTransform(angle, ptCenter.X, ptCenter.Y);
			
			poly.Points.Clear();
			poly.Points.Add(ptCenter);
			double len = Math.Sqrt(Math.Pow(Math.Abs(to.X-from.X), 2) + Math.Pow(Math.Abs(to.Y-from.Y), 2));
			poly.Points.Add(new Point(len, ptCenter.Y));
			poly.Points.Add(new Point(len - ARROWHEAD_LENGTH, poly.StrokeThickness));
			poly.Points.Add(new Point(len, ptCenter.Y));
			poly.Points.Add(new Point(len - ARROWHEAD_LENGTH, poly.StrokeThickness + ARROWHEAD_WIDTH));
			poly.Points.Add(new Point(len, ptCenter.Y));

			poly.Width = len;
			poly.Height = ARROWHEAD_WIDTH + (poly.StrokeThickness * 2);
		}

	}
}
